package com.android.gtdroid;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TaskDetailAct extends Activity {
	private TextView tv_dtl_prior;
	private TextView tv_dtl_task;
	private TextView tv_dtl_list;
	private TextView tv_dtl_prj;
	private LinearLayout ll_dtl_tags;
	private TextView tv_dtl_created;
	private TextView tv_dtl_se;
	private TextView tv_dtl_repeat;
	private TextView tv_dtl_remind;
	
	private GTDApp ga;
	private Calendar calCur;
	private HashMap<String, Object> hmCur;
	private int listIndex;
	private int taskStatus;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.task_detail);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_cmn);
		
		ga = (GTDApp) getApplication();
		calCur = Calendar.getInstance();
		hmCur = (HashMap<String, Object>) getIntent().getSerializableExtra("selected_task");
		listIndex = (int) getIntent().getIntExtra("list_index", 0);
		taskStatus = (int) getIntent().getIntExtra("task_status", C.TASK.UNCOMPLETED);
		
		initView();
		
		fillData();
	}
	
	private void fillData() {
		int flag;
		String prior = (String) hmCur.get("item_prior");
		
		if (prior.equals(getString(R.string.prior_none)))
			flag = 0;
		else if (prior.equals(getString(R.string.prior_high)))
			flag = 1;
		else if (prior.equals(getString(R.string.prior_medium)))
			flag = 2;
		else	
			flag = 3;
		
		switch(flag) {
			case 0:
				tv_dtl_prior.setBackgroundColor(0xff444444);
				break;
				
			case 1:
				tv_dtl_prior.setBackgroundColor(0xffff0000);
				break;
				
			case 2:
				tv_dtl_prior.setBackgroundColor(0xff0000ff);
				break;
				
			case 3:
				tv_dtl_prior.setBackgroundColor(0xff87cefa);
				break;
				
			default:
				break;
		}
		
		tv_dtl_task.setText((String) hmCur.get("item_name"));
		String strList = (String) hmCur.get("item_list");
		tv_dtl_list.setText(strList);
		
		String project = (String) hmCur.get("item_project");
		if (project.equals(ga.getProjects().get(0)))
			tv_dtl_prj.setText("");
		else
			tv_dtl_prj.setText("#"+project);
		
		String strTag = ((String) hmCur.get("item_tags"));
//		String strTag = "tga1,tag2,tag3,tag4";
//		String strTag = "tag";
		if (strTag.length() > 0) {
			String[] tags = strTag.split(",");		// if only one tag, return this tag
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(MarginLayoutParams.WRAP_CONTENT,
					MarginLayoutParams.WRAP_CONTENT);
			lp.setMargins(0, 0, 8, 0);
			
			// at least one string
			for (int i=0; i<tags.length; i++) {
				TextView tv = new TextView(this);
				tv.setText(tags[i]);
				tv.setPadding(2, 2, 2, 2);
				tv.setBackgroundColor(0xff888888);
				ll_dtl_tags.addView(tv,lp);
			}
		} else {
			LinearLayout ll_head = ((LinearLayout) findViewById(R.id.ll_head));
			ll_head.removeView(ll_dtl_tags);
		}
		tv_dtl_created.setText((String) hmCur.get("item_created"));
		
		String context = (String) hmCur.get("item_context");
		String pos = (String) hmCur.get("item_pos");
		
		// agenda
		LinearLayout ll_second = ((LinearLayout) findViewById(R.id.ll_second));
		LinearLayout ll_dtl_agenda = ((LinearLayout) findViewById(R.id.ll_dtl_agenda));
		tv_dtl_remind = ((TextView) findViewById(R.id.tv_dtl_remind));
		String remind = (String) hmCur.get("item_remind");
		if (strList.equals(getString(R.string.agenda))) {
			tv_dtl_se = ((TextView) findViewById(R.id.tv_dtl_se));
			int status = (Integer) hmCur.get("item_status");
			String time;
			
			if (status == 1) {		// all day task
				time = (((String) hmCur.get("item_st")).split(" "))[0]
						+ " ~ " + (((String) hmCur.get("item_et")).split(" "))[0];
			}
			else {
				String endTime = (String) hmCur.get("item_et");
				if (endTime.length() > 1)   // end time may be " " 
					time = (String) hmCur.get("item_st") + " ~ " + endTime;
				else
					time = (String) hmCur.get("item_st");
			}
			tv_dtl_se.setText(time);
			
			tv_dtl_repeat = ((TextView) findViewById(R.id.tv_dtl_repeat));
			String repeat = (String) hmCur.get("item_repeat");
			if (repeat.length() == 0) {
				ll_dtl_agenda.removeView(tv_dtl_repeat);
			} else {
				tv_dtl_repeat.setText(repeat+" �ظ�");
			}
			
			if (remind.length() == 0) {		// no remind in this task
				ll_dtl_agenda.removeView(tv_dtl_remind);
			} else {
				if (status == 1)	// all day task
					tv_dtl_repeat.setText("�� "+remind+" ����");
				else
					tv_dtl_repeat.setText(remind+" ����");
			}
			
			if (context.length() == 0)
				ll_second.removeView(((TextView) findViewById(R.id.tv_divider1)));
		} else {
			if (remind.length() == 0) {		// remove time textview
				ll_second.removeView(ll_dtl_agenda);
				ll_second.removeView(((TextView) findViewById(R.id.tv_divider1)));
				
			} else {
				tv_dtl_remind.setText("于 "+remind+" 提醒");
			}
		}
		
		// context
		TextView tv_dtl_context = ((TextView) findViewById(R.id.tv_dtl_context));
		if (context.equals(ga.getContexts().get(0))) {		// no context
			LinearLayout ll_dtl_context = ((LinearLayout) findViewById(R.id.ll_dtl_context));
			ll_second.removeView(ll_dtl_context);
			ll_second.removeView(((TextView) findViewById(R.id.tv_divider2)));
		}
		else {
			tv_dtl_context.setText(context);
			
			if (pos.equals(ga.getPositions().get(0)))
				ll_second.removeView(((TextView) findViewById(R.id.tv_divider2)));
		}
		
		// position
		TextView tv_dtl_pos = ((TextView) findViewById(R.id.tv_dtl_pos));
		if (pos.equals(ga.getPositions().get(0))) {
			LinearLayout ll_pos = ((LinearLayout) findViewById(R.id.ll_pos));
			ll_second.removeView(ll_pos);
		}
		else {
			tv_dtl_pos.setText(pos);
		}
		
		// task note
		TextView tv_dtl_note = ((TextView) findViewById(R.id.tv_dtl_note));
		String note = (String) hmCur.get("item_note");
		if (note.length() > 0)
			tv_dtl_note.setText(note);
		else {
			LinearLayout ll_content = ((LinearLayout) findViewById(R.id.ll_content));
			LinearLayout ll_dtl_note = ((LinearLayout) findViewById(R.id.ll_dtl_note));
			ll_content.removeView(ll_dtl_note);
		}
	}
	
	private void initView() {
		((TextView) findViewById(R.id.tv_title)).setText("查看任务");
		((TextView) findViewById(R.id.btn_new)).setVisibility(View.INVISIBLE);
		
		Button btn_return = ((Button) findViewById(R.id.btn_return));
		btn_return.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		tv_dtl_prior = ((TextView) findViewById(R.id.tv_dtl_prior));
		tv_dtl_task = ((TextView) findViewById(R.id.tv_dtl_task));
		tv_dtl_list = ((TextView) findViewById(R.id.tv_dtl_list));
		tv_dtl_prj = ((TextView) findViewById(R.id.tv_dtl_prj));
		ll_dtl_tags = ((LinearLayout) findViewById(R.id.ll_dtl_tags));
		tv_dtl_created = ((TextView) findViewById(R.id.tv_dtl_created));
		
		if (taskStatus == C.TASK.CANCELLED) {
			LinearLayout ll_dtl_top = ((LinearLayout) findViewById(R.id.ll_dtl_top));
			LinearLayout ll_btn = ((LinearLayout) findViewById(R.id.ll_btn));
			ll_dtl_top.removeView(ll_btn);
		} else {
			Button btn_move = ((Button) findViewById(R.id.btn_move));
			btn_move.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ArrayList<String> als = new ArrayList<String>();
					ArrayList<ListItem> al = ga.getListItems();
					
					for (int i=0; i<al.size(); i++)
						als.add(al.get(i).getListName());
					final String[] listItems = (String[]) als.toArray(new String[als.size()]);
					
					AlertDialog.Builder builder = new AlertDialog.Builder(TaskDetailAct.this);
					builder.setTitle("�ƶ���");
					builder.setSingleChoiceItems(listItems, listIndex, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int item) {
							// TODO Auto-generated method stub
							if (item != listIndex) {
								long posInTable = getIntent().getLongExtra("row_id", 0);
								TaskItem ti = ga.getDbta().getTaskItem(posInTable);
								if (item == GTDApp.AGENDA) {	// move a non-agenda task to agenda task
									ti.setAllDay(true);
									ti.setStartTime((new SimpleDateFormat("yyyy-MM-dd")).format(calCur.getTime()));
									ti.setEndTime((new SimpleDateFormat("yyyy-MM-dd")).format(calCur.getTime()));	// no deadline
									ti.setRepeat("");	// no repeat
								}
								ti.setListName(listItems[item]);
								ga.getDbta().updateTask(posInTable, ti);
		//						updateAdapterData();
								
								dialog.dismiss();
								finish();
							} else {
								dialog.dismiss();
							}
						}
					});
					builder.show();
				}
			});
			
			Button btn_del = ((Button) findViewById(R.id.btn_del));
			btn_del.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog.Builder builder = new AlertDialog.Builder(TaskDetailAct.this);
					builder.setTitle("确定要删除该任务吗");
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							long posInTable = getIntent().getLongExtra("row_id", 0);
							TaskItem ti = ga.getDbta().getTaskItem(posInTable);
							
							ti.setCompletedStatus(C.TASK.CANCELLED);
							ga.getDbta().updateTask(posInTable, ti);
							
							dialog.dismiss();
							finish();
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.dismiss();
						}
					});
					
					builder.show();
				}
			});
			
			Button btn_edit = ((Button) findViewById(R.id.btn_edit));
			btn_edit.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent itt = new Intent(TaskDetailAct.this, NewTaskAct.class);
					itt.putExtra("task_data", hmCur);
					itt.putExtra("list_index", listIndex);
					
					startActivityForResult(itt, 0);
					
					finish();
				}
			});
		}
	}
}
